class RightsController < ApplicationController
	    layout 'standard';
		
	before_filter :authorize,:authorizeSurveyer
	#before_filter :authorizeAuthor,:except => :rights_overview
	
	
	def rights_overview
		@survey = Survey.find(params[:id])
		respond_to do |format|
			format.html 
			format.xml { render :xml => @survey  }
		end
	end
	

	def set_rights
		
		@survey = Survey.find(params[:id])
		@users  =  User.all_not_temp
		@temps =User.all_temp
		@author_id =@survey.user.id
		@rights =  SurveyPermissionType.find(:all, :order => :id)
		@fill_id	=SurveyPermissionType.find_by_name("fill").id
		for user in @users
			3.times{
				@survey.rights.build
			}
			
		end
		
		respond_to do |format|
				format.html
				format.xml { render :xml => @user }
		end
		
		
	end

	
	def create_delete_rights
	
		@users  =  User.find(:all, :order => :login)
		@rights =  SurveyPermissionType.find(:all, :order => :ID)
	
		@survey=Survey.find(params[:id])
		if params[:existing]!=nil
			for right in @survey.rights
				@ss=right.userID.to_s<<"x"<<right.permissionTypeID.to_s
				if (params[:existing][@ss]==nil)
						UsersSurveysPermission.delete_all("USER_ID=#{right.userID} AND SURVEY_ID=#{params[:surveyID]} AND PERMISSION_TYPE_ID=#{right.permissionTypeID} 
													")
							
				end
				
			end
			
		else	
			UsersSurveysPermission.delete_all("SURVEY_ID=#{params[:surveyID]}" )
	 	end
		if params[:new]!=nil
			for newright in params[:new]
					 	@userAndPermission=newright[0].split('x')
						@right=UsersSurveysPermission.new(:userID=> @userAndPermission[0],:permissionTypeID=>@userAndPermission[1],:surveyID=>params[:surveyID])
						@right.save!	
			end
		end
		redirect_to :controller=>:rights,:action => :rights_overview,:id=>@survey.id		
		
	end	
	
	def get_logins_again
	  	survey=Survey.find(params[:id])	
	  	name=survey.title		
	  	fileName=Time.now.strftime("%Y%m%d_%H%M%S_")<<name.downcase<<"_all.txt"				
	  	header="Wszystkie automatycznie wygenerowane loginy i hasła dla ankiety '#{name}'\n\n"	
	  	temp_id=SystemRole.find_by_name("TempSurveyee").id
	  	users=survey.temp_ent_to_fill
		create_logins_file_again users,fileName,header
	    send_file 	fileName
	end


	def download_login_file
	  		amount = params[:number_of_temps].to_i	
	  		survey=Survey.find(params[:id])	
	  		name=survey.title		
	  		fileName=Time.now.strftime("%Y%m%d_%H%M%S_")<<name.downcase<<".txt"				
	  		header="Automatycznie wygenerowane loginy i hasła dla ankiety '#{name}' \n\n"	
			create_logins_file amount,fileName,header,survey.id
	    	send_file 	fileName

	end
	private
	def generate_temp_users
		@survey = Survey.find(params[:id])
		respond_to do |format|
				format.html
				format.xml { render :xml => @user }
		end
		
		
	end

	private 
	def create_logins_file amount,fileName,header,survey_id

		role=SystemRole.find_by_name('TempSurveyee').id
  		position=Position.find_by_name('Unknown').id  
		expiryDate=2.week.from_now	
		perm_id=SurveyPermissionType.find_by_name('fill').id
		file = File.new(fileName, "w")
		file.puts header
		file.puts "LOGIN/HASŁO\n\n\n"			
		amount.times{						
			login=generateWord
			password=generateWord	
			user=User.new(:login => login,:PASSWORD => password,
  					:position_id => position,  :role_id => role, :expiry_date =>expiryDate,:email=>"tempsurveyee@pollster.pl")	
  			user.save!		
			UsersSurveysPermission.create(:survey_id=>survey_id,:permission_type_id=>perm_id,:user_id=>user.id)
			file.puts login<<"\t\t\t\t\t"<< password
		}

		file.close
				
	end
	
	private 
	def create_logins_file_again users,fileName,header

		file = File.new(fileName, "w")
		file.puts header
		file.puts "LOGIN/HASŁO\n\n\n"	
		
		for user in users						
			newPassword=generateWord
			user.user.update_attributes!(:PASSWORD=>newPassword)
		 	file.puts user.user.login.to_s << "\t\t\t\t\t" <<  newPassword.to_s
			
		end

		file.close
				
	end
	
	private 
	def generateWord
		length=10
    	chars = ("a".."z").to_a+ ("0".."9").to_a
    	newpass = ""
    	1.upto(length) { |i| newpass << chars[rand(chars.size-1)] }
    	return newpass
    	
	end	
	
	
	
	
end
